package main

import (
	"fmt"
	"io/ioutil"
	"log"

	"gopkg.in/yaml.v2"
)

type Configuration struct {
	Global struct {
		ExternalLabels struct {
			Region  string `yaml:"region"`
			Replica string `yaml:"__replica__"`
		} `yaml:"external_labels"`
		ScrapeInterval     string `yaml:"scrape_interval"`
		EvaluationInterval string `yaml:"evaluation_interval"`
	} `yaml:"global"`
	RemoteWrite []struct {
		Url       string `yaml:"url"`
		BasicAuth struct {
			Username string `yaml:"username"`
			Password string `yaml:"password"`
		} `yaml:"basic_auth"`
		TlsConfig struct {
			InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
		} `yaml:"tls_config"`
	} `yaml:"remote_write"`
	ScrapeConfigs []struct {
		JobName        string `yaml:"job_name"`
		ScrapeInterval string `yaml:"scrape_interval"`
		ScrapeTimeout  string `yaml:"scrape_timeout"`
		Scheme         string `yaml:"scheme"`
		MetricsPath    string `yaml:"metrics_path"`
		TlsConfig      struct {
			InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
		} `yaml:"tls_config"`
		BearerTokenFile     string `yaml:"bearer_token_file"`
		KubernetesSdConfigs []struct {
			Role            string `yaml:"role"`
			ApiServer       string `yaml:"api_server"`
			BearerTokenFile string `yaml:"bearer_token_file"`
			TlsConfig       struct {
				InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
			} `yaml:"tls_config"`
		} `yaml:"kubernetes_sd_configs"`
		RelabelConfigs []struct {
			Action       string   `yaml:"action"`
			Regex        string   `yaml:"regex,omitempty"`
			SourceLabels []string `yaml:"source_labels"`
			TargetLabel  string   `yaml:"target_label,omitempty"`
			Replacement  string   `yaml:"replacement,omitempty"`
		} `yaml:"relabel_configs"`
		StaticConfigs []struct {
			Targets []string `yaml:"targets"`
			Labels  struct {
				CpuArch  string `yaml:"cpu_arch"`
				Password string `yaml:"password"`
				Instance string `yaml:"instance"`
				Zone     string `yaml:"zone"`
				Vendor   string `yaml:"vendor"`
				BmcIp    string `yaml:"bmc_ip"`
				Id       string `yaml:"id"`
				Region   string `yaml:"region"`
				Group    string `yaml:"group"`
				Username string `yaml:"username"`
			} `yaml:"labels"`
		} `yaml:"static_configs"`
	} `yaml:"scrape_configs"`
}

func main2() {
	// 读取文件
	data, err := ioutil.ReadFile("E:\\application\\开发环境归档\\omc-prometheus-collector\\overlays\\tempnode\\tempregion\\omc-prometheus-collector-0.yaml")
	if err != nil {
		panic(err)
	}

	var config Configuration
	err = yaml.Unmarshal(data, &config)
	if err != nil {
		log.Fatalf("unmarshal error: %v", err)
	}

	// 将配置写回文件
	newYamlFile, err := yaml.Marshal(&config)
	if err != nil {
		log.Fatalf("marshal error: %v", err)
	}

	err = ioutil.WriteFile("demo2.yaml", newYamlFile, 0644)
	if err != nil {
		log.Fatalf("write error: %v", err)
	}
	fmt.Println("Insertion complete.")
}
